/**
****************************************************************************************
 * @Author: luoweiyong fortwater969@163.com
 * @Version: 
 * @Date: 2024-06-25 19:12:52
 * @LastEditors: luoweiyong fortwater969@163.com
 * @LastEditTime: 2024-06-25 19:12:52
 * @FilePath: \code\App\user_apps.c
 * @Description: 
 * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
****************************************************************************************
*/
#include "user_apps.h"
#include "stm8s_clk.h"

static void CLK_Config_HSI(void)
{
    CLK_DeInit();
    CLK_LSICmd(ENABLE);
    CLK_HSICmd(ENABLE);
    while (SET != CLK_GetFlagStatus(CLK_FLAG_HSIRDY));
    
    CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//HSI=16M
}

void DelayInit(void)
{
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, ENABLE);
    TIM4_TimeBaseInit(TIM4_PRESCALER_64, 249);
    TIM4_SelectOnePulseMode(TIM4_OPMODE_SINGLE);//单脉冲
}

void DelayMs(uint16_t ms)
{
    while(ms--)
    {
        TIM4->SR1 = (uint8_t)(~TIM4_FLAG_UPDATE);
        TIM4->CR1 |= TIM4_CR1_CEN;
        while((TIM4->SR1 & (uint8_t)(TIM4_FLAG_UPDATE)) == 0);
    }
}

void sys_init(void)
{
    disableInterrupts();
    CLK_Config_HSI();
    enableInterrupts();
}
